var express = require('express');
var router = express.Router();
const positionsService = require('../service/positionsService');

//新增取货
router.post('/', async function (req, res) {
    let { name, pisition, notes } = req.body;
    let data = await positionsService.insert({ name, pisition, notes });
    res.send(data);
});

//查询全部
router.get('/', async function (req, res) {
    let { type, value, current, pageSize } = req.query;
    let searchParams = {}; //创建对象
    if (value) {
        searchParams[type] = new RegExp(value); //模糊查询
    }
    let data = await positionsService.find({ current: current || 1, pageSize: pageSize || 5 }, searchParams); //语法糖简写
    res.send(data); //将数据响应到浏览器上

});
//查询ID
router.get('/:id', async function (req, res) {
    let { id } = req.params; //获取路径里面的变量使用params
    let data = await positionsService.findById(id); //语法糖简写
    res.send(data); //将数据响应到浏览器上

});
//删除学生通过ID
router.delete('/:id', async function (req, res) {
    let { id } = req.params; //获取路径里面的变量使用params
    let data = await positionsService.delete(id); //语法糖简写
    res.send(data); //将数据响应到浏览器上

});
//修改学生通过ID
router.put('/:id', async function (req, res) {
    let { id } = req.params; //获取路径里面的变量使用params
    let { name, pisition, notes  } = req.body; //解构对象
    let data = await positionsService.update({ _id: id,  name, pisition, notes }); //语法糖简写
    res.send(data); //将数据响应到浏览器上

});


module.exports = router;